package com.lucky.ebidding.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.lucky.ebidding.dto.*;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.ScoreService;
import com.lucky.ebidding.service.TbService;
import com.lucky.ebidding.service.ZbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:ScoreController
 * Package:com.lucky.ebidding.controller
 * Description:
 *
 * @Date:2020/5/17 17:39
 * @Author:857251389@qq.com
 */
@Controller
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private ZbService zbService;

    @Autowired
    private TbService tbService;

    @GetMapping("/scoreJump/{zbId}")
    public String scoreJump(@PathVariable(name = "zbId") Integer zbId,
                            Model model) {

        model.addAttribute("zbId", zbId);
        return "expert/score";
    }

    @ResponseBody
    @GetMapping("/tbInfoSelect")
    public TbScoreDTO tbInfoSelect(Integer zbId,HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        List<TbInfo> tbInfos = tbService.selectTbInfoById(zbId);
        ArrayList<TbAndZbIdAndPfDTO> tbAndZbIdAndPfDTOS = new ArrayList<>();
        for (TbInfo tbInfo : tbInfos) {
            ZbInfo zbInfo = zbService.selectById(tbInfo.getZbId());
            Integer pf = scoreService.selectPfByTbIdAndExpertId(tbInfo.getId(), user.getId());
            TbAndZbIdAndPfDTO tbAndZbIdAndPfDTO = new TbAndZbIdAndPfDTO();
            tbAndZbIdAndPfDTO.setTbInfo(tbInfo);
            tbAndZbIdAndPfDTO.setZbInfo(zbInfo);
            tbAndZbIdAndPfDTO.setPf(pf);
            tbAndZbIdAndPfDTOS.add(tbAndZbIdAndPfDTO);
        }
        TbScoreDTO tbScoreDTO = new TbScoreDTO();

        tbScoreDTO.setData(tbAndZbIdAndPfDTOS);
        tbScoreDTO.setCode(0);
        tbScoreDTO.setMsg("yes");
        tbScoreDTO.setCount(tbInfos.size());
        return tbScoreDTO;

    }

    @ResponseBody
    @GetMapping("/scoreTotal")
    public TbScoreDTO tbInfoSelects(HttpServletRequest request){
        int size = 0;
        User user = (User) request.getSession().getAttribute("user");
        List<ZbInfo> zbInfos = zbService.selectByUserId(user.getId());

        ArrayList<TbAndZbIdAndPfDTO> tbAndZbIdAndPfDTOS = new ArrayList<>();
        for (ZbInfo zbInfo : zbInfos) {
            List<TbInfo> tbInfos = tbService.selectTbInfoById(zbInfo.getId());
            size += tbInfos.size();
            for(TbInfo tbInfo : tbInfos){
                List<ScoreInfo> scoreInfos = scoreService.selectInfoByTbId(tbInfo.getId());
                ScoreTotal scoreTotal = scoreService.scoreTotal(scoreInfos);
                TbAndZbIdAndPfDTO tbAndZbIdAndPfDTO = new TbAndZbIdAndPfDTO();
                tbAndZbIdAndPfDTO.setTbInfo(tbInfo);
                tbAndZbIdAndPfDTO.setZbInfo(zbInfo);
                tbAndZbIdAndPfDTO.setPf(scoreTotal.getTotalScore());
                tbAndZbIdAndPfDTOS.add(tbAndZbIdAndPfDTO);
            }
        }

        List<TbAndZbIdAndPfDTO> collect = tbAndZbIdAndPfDTOS.stream().sorted(Comparator.comparing(TbAndZbIdAndPfDTO::getPf).reversed()).limit(2).collect(Collectors.toList());

        TbScoreDTO tbScoreDTO = new TbScoreDTO();

        tbScoreDTO.setData(collect);
        tbScoreDTO.setCode(0);
        tbScoreDTO.setMsg("yes");
        tbScoreDTO.setCount(collect.size());
        return tbScoreDTO;
    }

    @GetMapping("/scoreDetail")
    public String scoreDetail() {

        return "expert/score_detail";
    }


    @PostMapping("/insertScoreInfo")
    @ResponseBody
    public ResultDTO insertScoreInfo(Integer zbId, Integer tbId, Integer score, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        int i = scoreService.insert(zbId,tbId,score,user);
        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }
}
